﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Threading;
using Budgetsch.ViewModels.Savings;

namespace Budgetsch.Views.Savings
{
    public partial class SavingsView : Page
    {
        private SavingsVM m_vm;
        private SaveFileDialog m_dialog;

        public SavingsView()
        {
            InitializeComponent();
            m_vm = new SavingsVM();
            this.DataContext = m_vm;

            m_dialog = new SaveFileDialog();

            m_dialog.DefaultExt = ".xlsx";
            m_dialog.Filter = "Excel 2007-2010 spreadsheet|*.xlsx";
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            var addForm = new SavingDetailView(m_vm.GetNewAddSavingVM());
            addForm.Show();
            if (addForm.DialogResult == true)
            {
                m_vm.RefreshSavings();
            }
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            m_vm.RemoveSaving(SavingsGrid.SelectedItem);
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            var editForm = new SavingDetailView(m_vm.GetNewSavingEditorVM(SavingsGrid.SelectedItem));
            editForm.Show();
        }

        private void Excel_Click(object sender, RoutedEventArgs e)
        {
            bool? dialogResult = m_dialog.ShowDialog();

            if (dialogResult == true)
            {
                m_vm.ExcelFileReady += o =>
                                           {
                                               using (Stream fs = (Stream)m_dialog.OpenFile())
                                               {
                                                   fs.Write(o, 0, o.Length);
                                                   fs.Close();
                                               }
                                           };
                m_vm.GetExcelFileAsync();
            }
        }

        private void AddCategory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                m_vm.AddCategory(CategoryTextBox.Text);
                CategoryTextBox.Text = string.Empty;
            }
        }

        private void YearCalendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            if (e.AddedDate.HasValue)
            {
                m_vm.SelectedYear = e.AddedDate.Value.Year;
                YearCalendar.DisplayMode = CalendarMode.Decade;
            }
        }

        private void MonthCalendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            if (e.AddedDate.HasValue)
            {
                m_vm.SelectedMonth = e.AddedDate.Value.Month;
                var dt = new DispatcherTimer()
                             {
                                 Interval = new TimeSpan(0, 0, 0, 0, 10)
                             };
                dt.Tick += new EventHandler(dt_Tick);
                dt.Start();
            }
        }

        //cheating because when year -> month "zoom" happens, the displaymode change only happens AFTER the displaydatechanged event risen
        void dt_Tick(object sender, EventArgs e)
        {
            ((DispatcherTimer)sender).Stop();
            MonthCalendar.DisplayMode = CalendarMode.Year;
        }
    }
}
